package com.anyou.mall.pla.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.pla.domain.VisibleUserTypeRelation;
import com.anyou.mall.pla.mapper.VisibleUserTypeRelationMapper;
import com.anyou.mall.pla.service.IVisibleUserTypeRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.pla.mapper.PosterInfoMapper;
import com.anyou.mall.pla.domain.PosterInfo;
import com.anyou.mall.pla.service.IPosterInfoService;

/**
 * 广告弹窗管理Service业务层处理
 * 
 * @author gaochaohao
 * @date 2022-03-18
 */
@Service
public class PosterInfoServiceImpl extends ServiceImpl<PosterInfoMapper,PosterInfo> implements IPosterInfoService
{
    @Autowired
    private PosterInfoMapper posterInfoMapper;

    @Autowired
    private IPosterInfoService iPosterInfoService;

    @Autowired
    private VisibleUserTypeRelationMapper visibleUserTypeRelationMapper;

    @Autowired
    private IVisibleUserTypeRelationService iVisibleUserTypeRelationService;

    /**
     * 查询广告弹窗管理
     * 
     * @param posterId 广告弹窗管理主键
     * @return 广告弹窗管理
     */
    @Override
    public PosterInfo selectPosterInfoByPosterId(Long posterId)
    {
        PosterInfo posterInfo = posterInfoMapper.selectPosterInfoByPosterId(posterId);
        if (posterInfo != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("poster_id",posterId);
            List<VisibleUserTypeRelation> visibleUserTypeRelationList = iVisibleUserTypeRelationService.list(wrapper);
            if (visibleUserTypeRelationList != null) {
                posterInfo.setVisibleUserTypeList(visibleUserTypeRelationList);
            }
        }
        return posterInfo;
    }

    /**
     * 查询广告弹窗管理列表
     * 
     * @param posterInfo 广告弹窗管理
     * @return 广告弹窗管理
     */
    @Override
    public List<PosterInfo> selectPosterInfoList(PosterInfo posterInfo)
    {
        List<PosterInfo> posterInfoList = posterInfoMapper.selectPosterInfoList(posterInfo);
        if (posterInfoList != null) {
            posterInfoList = posterInfoList.stream().map(posterInfo1 -> {
                QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
                wrapper.eq("poster_id", posterInfo1.getPosterId());
                List<VisibleUserTypeRelation> visibleUserTypeRelationList = iVisibleUserTypeRelationService.list(wrapper);
                posterInfo1.setVisibleUserTypeList(visibleUserTypeRelationList);
                return posterInfo1;
            }).collect(Collectors.toList());

        }
        return posterInfoList;
    }

    /**
     * 新增广告弹窗管理
     * 
     * @param posterInfo 广告弹窗管理
     * @return 结果
     */
    @Override
    public boolean insertPosterInfo(PosterInfo posterInfo)
    {
        posterInfo.setCreateTime(DateUtils.getNowDate());
        posterInfo.setDelFlag("0");
        boolean save = iPosterInfoService.save(posterInfo);
        if (save) {
            if (posterInfo.getVisibleUserTypeList() != null) {
                List<VisibleUserTypeRelation> visibleUserTypeList = posterInfo.getVisibleUserTypeList();
                for (VisibleUserTypeRelation visibleUserTypeRelation : visibleUserTypeList) {
                    visibleUserTypeRelation.setPosterId(posterInfo.getPosterId());
                    visibleUserTypeRelationMapper.insertVisibleUserTypeRelation(visibleUserTypeRelation);
                }
            }
        }
        return save;
    }

    /**
     * 修改广告弹窗管理
     * 
     * @param posterInfo 广告弹窗管理
     * @return 结果
     */
    @Override
    public int updatePosterInfo(PosterInfo posterInfo)
    {
        posterInfo.setUpdateTime(DateUtils.getNowDate());
        if (posterInfo.getVisibleUserTypeList() != null) {
            iVisibleUserTypeRelationService.remove(new LambdaQueryWrapper<VisibleUserTypeRelation>().eq(VisibleUserTypeRelation::getPosterId,posterInfo.getPosterId()));
            List<VisibleUserTypeRelation> visibleUserTypeList = posterInfo.getVisibleUserTypeList();
            visibleUserTypeList.forEach(e -> {
                e.setPosterId(posterInfo.getPosterId());
            });
            iVisibleUserTypeRelationService.saveBatch(visibleUserTypeList);
        }
        return posterInfoMapper.updatePosterInfo(posterInfo);
    }

    /**
     * 批量删除广告弹窗管理
     * 
     * @param posterIds 需要删除的广告弹窗管理主键
     * @return 结果
     */
    @Override
    public int deletePosterInfoByPosterIds(Long[] posterIds)
    {
        if (posterIds != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.in("poster_id",posterIds);
            visibleUserTypeRelationMapper.delete(wrapper);
        }
        return posterInfoMapper.deletePosterInfoByPosterIds(posterIds);
    }

    /**
     * 删除广告弹窗管理信息
     * 
     * @param posterId 广告弹窗管理主键
     * @return 结果
     */
    @Override
    public int deletePosterInfoByPosterId(Long posterId)
    {
        if (posterId != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("poster_id",posterId);
            visibleUserTypeRelationMapper.delete(wrapper);
        }
        return posterInfoMapper.deletePosterInfoByPosterId(posterId);
    }

    @Override
    public ApiResult updatePosterStatus(Long posterId) {
        PosterInfo posterInfo = posterInfoMapper.selectPosterInfoByPosterId(posterId);
        if (posterId != null) {
            if ("1".equals(posterInfo.getPosterStatus())) {
                posterInfo.setPosterStatus("0");
            } else {
                posterInfo.setPosterStatus("1");
            }
            return ApiResult.success(posterInfoMapper.updatePosterInfo(posterInfo));
        } else {
            return ApiResult.error("修改失败");
        }
    }
}
